﻿///***************************************************************
/// 시  스  템 : SMFramework
/// 단위시스템 : Utility
/// 계      층 : Framework Utility
/// 프로그램명 : MailClient
/// 파  일  명 : MailClient.cs
/// 설      명 : 메일 유틸리티
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-07-02 송시명   최초 작성
///***************************************************************
///
using System;
using System.Text;
using System.Net.Mail;

using SMFramework.Data;
using SMFramework.Diagnostics;
using SMFramework.Configuration;

namespace SMFramework.Adapter
{
	/// <summary>
	/// 메일 유틸리티
	/// </summary>
	public class MailClient
	{
	
		#region Variables Declaration
		
		private static ErrorLog _mailLog;
		
		private string hostName = "127.0.0.1";
		private Encoding encoding = Encoding.UTF8;

		private string from = null;
		private string to = null;
		private string subject = null;
		private string body = null;
		private bool isBodyHtml = false;
		
		#endregion

		#region Property Declaration
		/// <summary>
		/// RemoteHostName
		/// </summary>
		public string RemoteHostName
		{
			get
			{
				return this.hostName;
			}
			set
			{
				this.hostName = value;
			}
		}

		/// <summary>
		/// BodyEncoding
		/// </summary>
		public Encoding BodyEncoding
		{
			get
			{
				return this.encoding;
			}
			set
			{
				this.encoding = value;
			}
		}
		
		#endregion

		#region Default Constuctor & Overrided Constructor
		/// <summary>
		/// MailClient 생성자
		/// </summary>
		public MailClient()
		{
		}

		/// <summary>
		/// MailClient 생성자(hostName)
		/// </summary>
		/// <param name="hostName"></param>
		public MailClient(string hostName)
		{
			this.hostName = hostName;
		}

		/// <summary>
		/// MailClient 생성자(hostName, encoding)
		/// </summary>
		/// <param name="hostName"></param>
		/// <param name="encoding"></param>
		public MailClient(string hostName, Encoding encoding)
		{
			this.hostName = hostName;
			this.encoding = encoding;
		}
		
		#endregion		
		
		#region SendMail
		
		/// <summary>
		/// 메일발송
		/// </summary>
		/// <param name="ps_from"></param>
		/// <param name="ps_to"></param>
		/// <param name="ps_subject"></param>
		/// <param name="ps_body"></param>
		/// <returns></returns>
		public string SendMail(string ps_from, string ps_to, string ps_subject, string ps_body)
		{
			return SendMail(ps_from, ps_to, ps_subject, ps_body, false);
		}
		
		/// <summary>
		/// 메일발송
		/// </summary>
		/// <param name="ps_from"></param>
		/// <param name="ps_to"></param>
		/// <param name="ps_subject"></param>
		/// <param name="ps_body"></param>
		/// <param name="pb_is_body_html"></param>
		/// <returns></returns>
		public string SendMail(string ps_from, string ps_to, string ps_subject, string ps_body, bool pb_is_body_html)
		{
			DataPack lo_dp = new DataPack();
			lo_dp.AddProperty("From", typeof(string), ps_from.Trim());
			lo_dp.AddProperty("To", typeof(string), ps_to.Trim());
			lo_dp.AddProperty("Subject", typeof(string), ps_subject.Trim());
			lo_dp.AddProperty("Body", typeof(string), ps_body.Trim());
			lo_dp.AddProperty("IsBodyHtml", typeof(string), pb_is_body_html.ToString());

			return SendMail(lo_dp);
		}
		
		/// <summary>
		/// 메일발송 - DataPack
		/// </summary>
		/// <param name="lo_dp"></param>
		/// <returns></returns>
		public string SendMail(DataPack lo_dp)
		{
			try
			{
				this.from = String.IsNullOrEmpty(lo_dp["From"].ToString()) ? "" : lo_dp["From"].ToString();
				this.to = String.IsNullOrEmpty(lo_dp["To"].ToString()) ? "" : lo_dp["To"].ToString();
				this.subject = String.IsNullOrEmpty(lo_dp["Subject"].ToString()) ? "NO TITLE" : lo_dp["Subject"].ToString();
				this.body = String.IsNullOrEmpty(lo_dp["Body"].ToString()) ? "NO CONTENTS" : lo_dp["Body"].ToString();
				this.isBodyHtml = String.IsNullOrEmpty(lo_dp["IsBodyHtml"].ToString()) ? false : Convert.ToBoolean(lo_dp["IsBodyHtml"].ToString());

				MailMessage msg = new MailMessage();
				msg.From = new MailAddress(from.Trim());
				msg.To.Add(new MailAddress(to.Trim()));
				msg.Subject = subject.Trim();
				msg.Body = body.Trim();
				msg.BodyEncoding = BodyEncoding;
				msg.IsBodyHtml = isBodyHtml;

				SmtpClient sc = new SmtpClient();
				sc.Host = hostName;
				sc.Send(msg);
				
				//로깅
				if (WebConfig.SMFramework["Mail.Logging"].ToUpper() == "TRUE")
				{
					_mailLog = new ErrorLog("Mail", "SMFramework", WebConfig.SMFramework["Mail.LogFile"]);
					
					DataPack lo_dp_header = new DataPack();

					string[] keys = msg.Headers.AllKeys;
					foreach (string s in keys)
					{
						lo_dp_header.AddProperty(s, typeof(string), msg.Headers[s]);
					}
					lo_dp_header.AddProperty("host", typeof(string), hostName);
					lo_dp_header.AddProperty("subject", typeof(string), subject);
					lo_dp_header.AddProperty("isBodyHtml", typeof(string), isBodyHtml.ToString());
					lo_dp_header.AddProperty("bodyEncoding", typeof(string), msg.BodyEncoding.BodyName);

					string ls_msg = Utility.ConversionUtil.ExtractDataInfo("--- SendMail Result ---", lo_dp_header);

					ls_msg = ls_msg.Replace("<br>", "\r\n");

					_mailLog.WriteLogMessage(ls_msg, System.Diagnostics.EventLogEntryType.Information);
				}
			}
			catch (Exception e)
			{
				_mailLog.WriteLogMessage(e.Message, System.Diagnostics.EventLogEntryType.Error);
				return e.Message;
			}
			return "OK";
		}

		#endregion
		
	}
}
